package com.meilai.project.vo.business.intentionScheme;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.meilai.project.dto.business.intentionScheme.*;
import com.meilai.project.entity.business.intentionScheme.IntentionScheme;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author xchen
 * @since 2022-02-27
 */
@Data
@ApiModel
public class IntentionSchemeVO extends IntentionScheme {
    @ApiModelProperty("是否转销售单")
    private Boolean hasBillSale = false;

    @ApiModelProperty("关联的销售单状态")
    private Integer billSaleStatus;

    @ApiModelProperty("合同金额(优惠价格)")
    private BigDecimal discounted_price;

    @ApiModelProperty(value = "合同名称")
    private String contract_name;

    @ApiModelProperty(value = "客户名称")
    private String customer_name;

    @ApiModelProperty("锁定人")
    private String lockUserName;

    @ApiModelProperty("维护人id")
    private Long maintenance_staff_id;

    @ApiModelProperty("维护人")
    private String maintenanceStaffName;

    @ApiModelProperty("锁定区间,多个使用逗号分割")
    private String cycle_date;

    @ApiModelProperty("方案类型名称")
    private String plan_type_name;

    @ApiModelProperty("daterange_id")
    private Long daterange_id;

    @ApiModelProperty("daterange_start_at")
    private LocalDate daterange_start_at;

    @ApiModelProperty("daterange_end_at")
    private LocalDate daterange_end_at;

    @ApiModelProperty("销售单变更前的合同的客户id")
    private Long before_update_sale_customer_id;

    @ApiModelProperty("销售单变更前的合同id")
    private Long before_update_sale_contract_id;

    @ApiModelProperty("点位信息，详情时候返回，列表返回时返回空")
    private List<PointProcessDateRangeVO> dateRangeVOList;

    @ApiModelProperty("媒体类型集合数据")
    private List<String> media_type_name_list;

    public IntentionSchemeSaveDTO to() {
        IntentionSchemeSaveDTO intentionSchemeSaveDTO = new IntentionSchemeSaveDTO();
        intentionSchemeSaveDTO.setId(this.getId());
        intentionSchemeSaveDTO.setChoose_id(this.getChoose_id());
        intentionSchemeSaveDTO.setPlan_name(this.getPlan_name());
        intentionSchemeSaveDTO.setPlan_type(this.getPlan_type());
        intentionSchemeSaveDTO.setRelated_type(this.getRelated_type());
        intentionSchemeSaveDTO.setRelated_id(this.getRelated_id());
        intentionSchemeSaveDTO.setCustomer_id(this.getCustomer_id());
        intentionSchemeSaveDTO.setSeller_id(this.getSeller_id());
        intentionSchemeSaveDTO.setMemo(this.getMemo());

        List<PointProcessDateRangeDTO> dataList = new ArrayList<>();
        for (PointProcessDateRangeVO dateRangeVO : this.dateRangeVOList) {
            PointProcessDateRangeDTO item = new PointProcessDateRangeDTO();
            item.setBeginDate(dateRangeVO.getBeginDate());
            item.setEndDate(dateRangeVO.getEndDate());
            List<MediaPointSimpleVO> mediaPointVOList = new ArrayList<>();
            List<PointProcessDateRangeDTO.ComboInfo> comboInfoList = new ArrayList<>();
            for (BuildingPointVO buildingPointVO : dateRangeVO.getBuildingPointVOList()) {
                for (Map.Entry<Long, PointTypeCountVO> entry : buildingPointVO.getPointTypeCountVOMap().entrySet()) {
                    mediaPointVOList.addAll(entry.getValue().getMediaPointList());
                }
                List<BuildingPointVO.ComboCountInfo> comboCountInfoList = buildingPointVO.getComboCountInfoList();
                if (CollUtil.isNotEmpty(comboCountInfoList)) {
                    PointProcessDateRangeDTO.ComboInfo comboInfo = new PointProcessDateRangeDTO.ComboInfo();
                    for (BuildingPointVO.ComboCountInfo comboCountInfo : comboCountInfoList) {
                        comboInfo.setBuilding_area_id(buildingPointVO.getId());
                        comboInfo.setCombo_id(comboCountInfo.getComboId());
                        for (BuildingPointVO.RuleCountInfo ruleCountInfo : comboCountInfo.getRuleCountList()) {
                            PointProcessDateRangeDTO.ComboInfo comboInfoItem = ObjectUtil.clone(comboInfo);
                            comboInfoItem.setRule_id(ruleCountInfo.getRuleId());
                            comboInfoItem.setCount(ruleCountInfo.getCount().longValue());
                            comboInfoList.add(comboInfoItem);
                        }
                    }
                }
            }
            item.setPointList(mediaPointVOList);
            item.setComboInfoList(comboInfoList);

            dataList.add(item);
        }

        intentionSchemeSaveDTO.setDataList(dataList);
        return intentionSchemeSaveDTO;
    }

}
